package cn.iocoder.yudao.module.resource.dal.dataobject.audiotone;

import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.resource.dal.dataobject.message.MessageDO;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.Map;

/**
 * 声纹管理 DO
 *
 * @author wgs
 */
@TableName(value = "lm_audio_tone", autoResultMap = true)
@KeySequence("lm_audio_tone_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库，可不写。
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AudioToneDO extends BaseDO {

    /**
     * 唯 一id
     */
    @TableId
    private Integer id;
    /**
     * 声纹名称
     */
    private String name;
    /**
     * 声纹文件id
     */
    private Long fileId;

    /**
     * 音色复刻id
     */
    private String cloneVoiceId;

    /**
     * 音色复刻对象
     */
    @TableField(typeHandler = CloneVoiceHandler.class)
    private CloneVoice cloneVoiceObj;

    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    public static class CloneVoice {
        private Map<String, String> ttsNameCloneVoiceIdMap;

    }

    public String getCloneVoiceIdByTssName(String tssName) {
        if (cloneVoiceObj == null) {
            return null;
        }
        return cloneVoiceObj.getTtsNameCloneVoiceIdMap().get(tssName);
    }

    public static class CloneVoiceHandler extends AbstractJsonTypeHandler<Object> {

        @Override
        protected Object parse(String json) {
            return JsonUtils.parseObject(json, CloneVoice.class);
        }

        @Override
        protected String toJson(Object obj) {
            return JsonUtils.toJsonString(obj);
        }

    }
}